package com.hmh.student.students.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.students.entity.Course;
import com.hmh.student.students.entity.Teacher;
import com.hmh.student.students.service.ICourseService;
import com.hmh.student.students.service.ITeacherService;
import com.hmh.student.system.result.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程管理前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-05-11
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ITeacherService teacherService;

    @GetMapping("/getAllTeachers")
    public Result getAllTeachers() {
        List<Teacher> teachers = teacherService.list();
        return Result.success(teachers);
    }

    @GetMapping("/{courseNo}")
     public Course getCourseById(Integer courseNo) {
        return courseService.getById(courseNo);
     }
    /**
     * 修改课程信息（包含教师编号验证）
     */
    @PutMapping("/updateCourse")
    public Map<String, Object> updateCourse(@RequestBody Course course) {
        Map<String, Object> map = new HashMap<>();
        try {
            // 验证教师编号是否存在
            if (course.getTeacherNo() != null) {
                Teacher teacher = teacherService.getById(course.getTeacherNo());

                if (teacher == null) {
                    map.put("msg", "更新失败: 指定的教师编号不存在");
                    return map;
                }
            }

            // 更新课程信息
            boolean isUpdated = courseService.updateById(course);
            if (isUpdated) {
                map.put("msg", "success");
                map.put("data", course);
            } else {
                map.put("msg", "更新失败: 未找到对应课程或数据未变更");
            }
        } catch (Exception e) {
            map.put("msg", "更新失败: " + e.getMessage());
        }
        return map;
    }



    /**
     * 添加课程
     */
    @PostMapping("/addCourse")
    public Map<String, Object> addCourse(@RequestBody Course course) {
        Map<String, Object> map = new HashMap<>();
        try {
            if (course.getTeacherNo() != null) {
                Teacher teacher = teacherService.getById(course.getTeacherNo());

                if (teacher == null) {
                    map.put("msg","新增失败: 指定的教师编号不存在");
                    return map;
                }
            }
            courseService.save(course);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "添加失败：" + e.getMessage());
        }
        return map;
    }

    /**
     * 删除课程（通过课程编号）
     */
    @DeleteMapping("/deleteCourse")
    public Map<String, Object> deleteCourse(@RequestParam Integer courseNo) {
        Map<String, Object> map = new HashMap<>();
        try {
            courseService.removeById(courseNo);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "删除失败：" + e.getMessage());
        }
        return map;
    }

    /**
     * 分页查询课程（支持按名称、教师编号筛选）
     */
    @GetMapping("/pageCourse")
    public Page<Course> pageCourse(
            @RequestParam(required = false) String courseName,
            @RequestParam(required = false) Integer teacherNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer current) {
        return courseService.queryCoursePage(courseName, teacherNo, pageSize, current);
    }


}